package cn.iocoder.yudao.module.member.assets.controller.app;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.convert.BigDecimalConvert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.api.cms.qlink.QlinkApi;
import cn.iocoder.yudao.module.api.cms.qlink.dto.QlinkRespDTO;
import cn.iocoder.yudao.module.member.assets.convert.MemberDigitalAssetsConvert;
import cn.iocoder.yudao.module.member.assets.dal.dataobject.MemberDigitalAssetsDO;
import cn.iocoder.yudao.module.member.assets.service.AppMemberDigitalAssetsService;
import cn.iocoder.yudao.module.member.assets.vo.AppMemberDigitalAssetsMenuRespVO;
import cn.iocoder.yudao.module.member.assets.vo.AppMemberDigitalAssetsRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 数字资产")
@RestController
@RequestMapping("/member/digital-assets")
@Validated
public class AppMemberDigitalAssetsController {
    @Resource
    private AppMemberDigitalAssetsService appMemberDigitalAssetsService;

    @Resource
    private QlinkApi qlinkApi;

    @GetMapping("/get")
    @Operation(summary = "获取数字资产")
    public CommonResult<AppMemberDigitalAssetsRespVO> getDetail() {
        MemberDigitalAssetsDO digitalAssetsDO = appMemberDigitalAssetsService.getOrCreate(getLoginUserId());
        return success(MemberDigitalAssetsConvert.INSTANCE.convert2(digitalAssetsDO));
    }

    @GetMapping("/get-menu")
    @Operation(summary = "获取菜单")
    @PermitAll
    public CommonResult<List<AppMemberDigitalAssetsMenuRespVO>> getMenu() {
        List<AppMemberDigitalAssetsMenuRespVO> menus = new ArrayList<>();
        List<QlinkRespDTO> qlinks = qlinkApi.getQlinkByIdentify("yunmengzhongxingcaidan");
        for (QlinkRespDTO qlink : qlinks) {
            AppMemberDigitalAssetsMenuRespVO menu = new AppMemberDigitalAssetsMenuRespVO();
            menu.setSubIdentify(qlink.getSubIdentify());
            menu.setTitle(qlink.getTitle());
            menu.setPicUrl(qlink.getPicUrl());
            menu.setLinkUrl(qlink.getLinkUrl());
            menu.setNumber(BigDecimalConvert.INSTANCE.convert(0));
            menus.add(menu);
        }
        Long userId = getLoginUserId();
        if (userId == null || userId <= 0) {
            return success(menus);
        }
        MemberDigitalAssetsDO digitalAssetsDO = appMemberDigitalAssetsService.getOrCreate(userId);
        for (AppMemberDigitalAssetsMenuRespVO menu : menus) {
            switch (StrUtil.nullToDefault(menu.getSubIdentify(), "")) {
                case "lianmengdou":
                    menu.setNumber(BigDecimalConvert.INSTANCE.convert(digitalAssetsDO.getCryptocurrencyOrigin()));
                    break;
                case "shouyidou":
                    menu.setNumber(BigDecimalConvert.INSTANCE.convert(digitalAssetsDO.getCryptocurrencyIncrement()));
                    break;
                case "fenhongdou":
                    menu.setNumber(BigDecimalConvert.INSTANCE.convert(digitalAssetsDO.getBonus()));
                    break;
                case "gongyixunzhang":
                    menu.setNumber(BigDecimalConvert.INSTANCE.convert(digitalAssetsDO.getShareholding()));
                    break;
                default:
                    break;
            }
        }
        return success(menus);
    }

}